package com.ruoyi.deeplearn.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.deeplearn.domain.*;
import com.ruoyi.deeplearn.mapper.DeepMarkTaskDatesetMapper;
import com.ruoyi.deeplearn.service.IDeepMarkTaskService;
import com.ruoyi.deeplearn.service.IDeepModelSampleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.deeplearn.mapper.DeepModelSampleMapper;
import com.ruoyi.deeplearn.service.IDeepModelSampleService;

/**
 * 模型样本Service业务层处理
 *
 * @author rww
 * @date 2024-09-07
 */
@Service
public class DeepModelSampleServiceImpl implements IDeepModelSampleService
{
    @Autowired
    private DeepModelSampleMapper deepModelSampleMapper;
    @Autowired
    private IDeepModelSampleTaskService deepModelSampleTaskService;
    @Autowired
    private IDeepMarkTaskService deepMarkTaskService;
    @Autowired
    private DeepMarkTaskDatesetMapper deepMarkTaskDatesetMapper;
    /**
     * 查询模型样本
     *
     * @param id 模型样本主键
     * @return 模型样本
     */
    @Override
    public DeepModelSample selectDeepModelSampleById(Long id)
    {
        return deepModelSampleMapper.selectDeepModelSampleById(id);
    }

    /**
     * 查询模型样本列表
     *
     * @param deepModelSample 模型样本
     * @return 模型样本
     */
    @Override
    public List<DeepModelSample> selectDeepModelSampleList(DeepModelSample deepModelSample)
    {
        return deepModelSampleMapper.selectDeepModelSampleList(deepModelSample);
    }

    /**
     * 新增模型样本
     *
     * @param deepModelSample 模型样本
     * @return 结果
     */
    @Override
    public int insertDeepModelSample(DeepModelSample deepModelSample)
    {
        deepModelSample.setId(IdUtil.getSnowflakeNextId());
        deepModelSample.setCreateTime(DateUtils.getNowDate());
        return deepModelSampleMapper.insertDeepModelSample(deepModelSample);
    }

    /**
     * 修改模型样本
     *
     * @param deepModelSample 模型样本
     * @return 结果
     */
    @Override
    public int updateDeepModelSample(DeepModelSample deepModelSample)
    {
        deepModelSample.setUpdateTime(DateUtils.getNowDate());
        return deepModelSampleMapper.updateDeepModelSample(deepModelSample);
    }

    /**
     * 批量删除模型样本
     *
     * @param ids 需要删除的模型样本主键
     * @return 结果
     */
    @Override
    public int deleteDeepModelSampleByIds(Long[] ids)
    {
        return deepModelSampleMapper.deleteDeepModelSampleByIds(ids);
    }

    /**
     * 删除模型样本信息
     *
     * @param id 模型样本主键
     * @return 结果
     */
    @Override
    public int deleteDeepModelSampleById(Long id)
    {
        return deepModelSampleMapper.deleteDeepModelSampleById(id);
    }

    @Override
    public int createDeepModelSample(DeepModelSample deepModelSample) {
        insertDeepModelSample(deepModelSample);
        if(deepModelSample.getMarkTasks()!=null){
            deepModelSample.getMarkTasks().stream().forEach(taskId->{
                DeepMarkTask deepMarkTask = deepMarkTaskService.selectDeepMarkTaskById(taskId);
                DeepModelSampleTask sampleTask = new DeepModelSampleTask();
                sampleTask.setSampleId(deepModelSample.getId());
                sampleTask.setTaskId(taskId);
                sampleTask.setModuleId(deepModelSample.getModuleId());
                sampleTask.setMarkProjectId(deepMarkTask.getMarkProjectId());
                sampleTask.setTagGroupId(deepMarkTask.getTagGroupId());
                deepModelSampleTaskService.insertDeepModelSampleTask(sampleTask);
            });
        }
        return 1;
    }

    @Override
    public List<DeepMarkTaskDateset> selectSampleDataList(DeepModelSample deepModelSample) {
        return deepMarkTaskDatesetMapper.selectSampleDataListBySampleId(deepModelSample.getId());
    }
}
